package iscript.uiFramework.components.panel
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.geom.Rectangle;
	import iscript.uiFramework.components.UIComponent;
	import iscript.uiFramework.util.UIComponentUtil;
	
	public class FramePanel extends UIComponent
	{
		private var _borderLayout:FramePanelLayoutControl;
		private var _northItem:DisplayObject;
		private var _westItem:DisplayObject;
		private var _centerItem:DisplayObject;
		private var _eastItem:DisplayObject;
		private var _southItem:DisplayObject;
		
		private var _minBound:Rectangle;
		private var _bound:Rectangle;
		
		static public const NORTH:String = "north";
		static public const WEST:String = "west";
		static public const CENTER:String = "center";
		static public const EAST:String = "east";
		static public const SOUTH:String = "south";
		
		public function FramePanel(preferWidth:Number = 200, preferHeight:Number = 150)
		{
			_bound = new Rectangle(0, 0, preferWidth, preferHeight)
			_borderLayout = new FramePanelLayoutControl(_bound)
			_minBound = _borderLayout.minRect;
			_borderLayout.northHeight = 0;
			_borderLayout.westWidth = 0;
			_borderLayout.eastWidth = 0;
			_borderLayout.southHeight = 0;
			super(preferWidth, preferHeight);
		}
		
		/**
		 * 更新布局
		 * @param	bound
		 */
		private function update(bound:Rectangle = null):void
		{
			_borderLayout.updateRect(bound);
			_elementUpdateLogic();
		}
		
		override public function render(container:DisplayObjectContainer = null):void
		{
			update();
			if (_northItem != null)
			{
				setChildIndex(_northItem, this.numChildren - 1);
			}
			super.render(container);
		}
		
		private function _elementUpdateLogic():void
		{
			if (_northItem != null && _borderLayout.northHeight > 0)
			{
				_northItem.x = _borderLayout.northRect.x;
				_northItem.y = _borderLayout.northRect.y;
				_northItem.width = _borderLayout.northRect.width;
				_northItem.height = _borderLayout.northRect.height;
				_northItem.visible = true;
			}
			if (_northItem != null && _borderLayout.northHeight <= 0)
			{
				_northItem.visible = false;
			}
			
			if (_westItem != null && _borderLayout.westRect.width > 0)
			{
				_westItem.width = _borderLayout.westRect.width;
				_westItem.height = _borderLayout.westRect.height;
				_westItem.x = _borderLayout.westRect.x;
				_westItem.y = _borderLayout.westRect.y;
				_westItem.visible = true;
			}
			if (_westItem != null && _borderLayout.westRect.width <= 0)
			{
				_westItem.visible = false;
			}
			
			if (_centerItem != null && _borderLayout.centerRect.width > 0)
			{
				_centerItem.width = _borderLayout.centerRect.width;
				_centerItem.height = _borderLayout.centerRect.height;
				_centerItem.x = _borderLayout.centerRect.x;
				_centerItem.y = _borderLayout.centerRect.y;
				_centerItem.visible = true;
			}
			if (_centerItem != null && _borderLayout.centerRect.width <= 0)
			{
				_centerItem.visible = false;
			}
			
			if (_eastItem != null && _borderLayout.eastRect.width > 0)
			{
				_eastItem.width = _borderLayout.eastRect.width;
				_eastItem.height = _borderLayout.eastRect.height;
				_eastItem.x = _borderLayout.eastRect.x;
				_eastItem.y = _borderLayout.eastRect.y;
				_eastItem.visible = true;
			}
			if (_eastItem != null && _borderLayout.eastRect.width <= 0)
			{
				_eastItem.visible = false;
			}
			
			if (_southItem != null && _borderLayout.southRect.width > 0)
			{
				_southItem.width = _borderLayout.southRect.width;
				_southItem.height = _borderLayout.southRect.height;
				_southItem.x = _borderLayout.southRect.x;
				_southItem.y = _borderLayout.southRect.y;
				_southItem.visible = true;
			}
			if (_southItem != null && _borderLayout.eastRect.width <= 0)
			{
				_southItem.visible = false;
			}
		}
		
		/**
		 * 最小范围
		 */
		public function get minBound():Rectangle
		{
			return _minBound;
		}
		
		/**
		 * 最小范围
		 */
		public function set minBound(value:Rectangle):void
		{
			_minBound = value;
			_borderLayout.minRect = value;
		}
		
		/**
		 * 北高度
		 */
		public function set northHeight(value:Number):void
		{
			_borderLayout.northHeight = value;
			if (IsRender)
				update();
		
		}
		
		/**
		 * 南高度
		 */
		public function set southHeight(value:Number):void
		{
			_borderLayout.southHeight = value;
			if (IsRender)
				update();
		
		}
		
		public function set westWidth(value:Number):void
		{
			_borderLayout.westWidth = value;
			if (IsRender)
				update();
		
		}
		
		public function set eastWidth(value:Number):void
		{
			_borderLayout.eastWidth = value;
			if (IsRender)
				update();
		}
		
		public function get bound():Rectangle
		{
			return _bound;
		}
		
		public function set bound(value:Rectangle):void
		{
			_bound = value;
			_borderLayout.updateRect()
			if (IsRender)
				update();
		}
		
		override public function set width(value:Number):void
		{
			_preferWidth = value
			_bound = new Rectangle(0, 0, _preferWidth, _preferHeight)
			update(_bound);
			super.width = value;
		}
		
		override public function set height(value:Number):void
		{
			_preferHeight = value
			_bound = new Rectangle(0, 0, _preferWidth, _preferHeight)
			update(_bound);
			super.height = value;
		}
		
		public function append(value:DisplayObject, pos:String):void
		{
			switch (pos)
			{
				case FramePanel.NORTH: 
					_northItem = value;
					_northItem.width = _borderLayout.northRect.width;
					if (_borderLayout.northHeight == 0)
						_borderLayout.northHeight = _northItem.height;
					break;
				case FramePanel.WEST: 
					_westItem = value;
					_westItem.width = _borderLayout.westRect.width;
					_westItem.height = _borderLayout.westRect.height;
					if (_borderLayout.westWidth == 0)
						_borderLayout.westWidth = _westItem.width;
					break;
				case FramePanel.CENTER: 
					_centerItem = value;
					_centerItem.width = _borderLayout.centerRect.width;
					_centerItem.height = _borderLayout.centerRect.height;
					break;
				case FramePanel.EAST: 
					_eastItem = value;
					_eastItem.width = _borderLayout.eastRect.width;
					_eastItem.height = _borderLayout.eastRect.height;
					if (_borderLayout.eastWidth == 0)
						_borderLayout.eastWidth = _eastItem.width;
					break;
				case FramePanel.SOUTH: 
					_southItem = value;
					_southItem.width = _borderLayout.southRect.width;
					_southItem.height = _borderLayout.southRect.height;
					if (_borderLayout.southHeight == 0)
						_borderLayout.southHeight = _southItem.height;
					break;
			}
			UIComponentUtil.addChild(value, this);
		}
		
		public function remove(pos:String):void
		{
			switch (pos)
			{
				case FramePanel.NORTH: 
					_removeElementLogic(_northItem);
					northHeight = 0;
					break;
				case FramePanel.WEST: 
					_removeElementLogic(_westItem);
					westWidth=0
					break;
				case FramePanel.CENTER: 
					_removeElementLogic(_centerItem);
					break;
				case FramePanel.EAST: 
					_removeElementLogic(_eastItem);
					eastWidth = 0
					break;
				case FramePanel.SOUTH: 
					_removeElementLogic(_southItem);
					southHeight = 0;
					break;
			}
		}
		
		private function _removeElementLogic(target:DisplayObject):void
		{
			if (target)
			{
				if (target is UIComponent)
					UIComponent(target).dispose();
				else
					removeChild(target)
			}
		}
	
	}

}